package com.fram.service.admin.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProValiDataException;
import com.fram.entity.Checkversion;
import com.fram.mapper.CheckversionMapper;
import com.fram.service.admin.CheckversionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class CheckversionServiceImpl implements CheckversionService{
	
	@Autowired
	private CheckversionMapper checkversionMapper;
	
	@Override
	public PageInfo<Checkversion> list(PageBean<Checkversion> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<Checkversion> list = checkversionMapper.listForAdmin(request.getParam());
		if(list == null || list.size()==0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}
	
	@Override
	public void add(Checkversion request)throws ProException {
		if(request.getIsOpen()==1) {
			List<Checkversion> list =checkversionMapper.select(request);
			for (Checkversion checkversion : list) {
				if(checkversion.getIsOpen()==1) {
					checkversion.setIsOpen(0);
					if(checkversionMapper.updateByPrimaryKeySelective(checkversion) == 0) {
						throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
					}
				}
			}
		}
		if(checkversionMapper.insertSelective(request)==0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public void update(Checkversion request)throws ProException {
		if(request.getIsOpen()==1) {
			List<Checkversion> list =checkversionMapper.select(request);
			for (Checkversion checkversion : list) {
				if(checkversion.getIsOpen()==1) {
					checkversion.setIsOpen(0);
					if(checkversionMapper.updateByPrimaryKeySelective(checkversion) == 0) {
						throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
					}
				}
			}
		}
		if(checkversionMapper.updateByPrimaryKeySelective(request)==0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public Checkversion details(Long id) throws ProException {
		Checkversion request = checkversionMapper.selectByPrimaryKey(id);
		if(request==null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return request;
	}
}
